package org.jeecg.modules.demo.oa.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aliyun.oss.common.utils.StringUtils;
import com.sun.tools.javac.util.Assert;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.oa.entity.ComApprovalList;
import org.jeecg.modules.demo.oa.entity.OaHetongShenpi;
import org.jeecg.modules.demo.oa.service.IComApprovalListService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 审批清单
 * @Author: jeecg-boot
 * @Date:   2024-09-13
 * @Version: V1.0
 */
@Api(tags="审批清单")
@RestController
@RequestMapping("/oa/comApprovalList")
@Slf4j
public class ComApprovalListController extends JeecgController<ComApprovalList, IComApprovalListService> {
	@Autowired
	private IComApprovalListService comApprovalListService;
	
	/**
	 * 分页列表查询
	 *
	 * @param comApprovalList
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "审批清单-分页列表查询")
	@ApiOperation(value="审批清单-分页列表查询", notes="审批清单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="modules/oa/ComApprovalList/ComApprovalList")
	public Result<IPage<ComApprovalList>> queryPageList(ComApprovalList comApprovalList,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ComApprovalList> queryWrapper = QueryGenerator.initQueryWrapper(comApprovalList, req.getParameterMap());
		Page<ComApprovalList> page = new Page<ComApprovalList>(pageNo, pageSize);
		IPage<ComApprovalList> pageList = comApprovalListService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param comApprovalList
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "我的审批清单-分页列表查询")
	 @ApiOperation(value="我的审批清单-分页列表查询", notes="我的审批清单-分页列表查询")
	 @GetMapping(value = "/listMy")
	 public Result<IPage<ComApprovalList>> queryPageListMy(ComApprovalList comApprovalList,
														 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														 @RequestParam(name="pageSize", defaultValue="5") Integer pageSize,
														 HttpServletRequest req) {

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 comApprovalList.setShenpiBy( sysUser.getUsername()); //查询当前登录人的审批清单

		 QueryWrapper<ComApprovalList> queryWrapper = QueryGenerator.initQueryWrapper(comApprovalList, req.getParameterMap());
		 Page<ComApprovalList> page = new Page<ComApprovalList>(pageNo, pageSize);
		 IPage<ComApprovalList> pageList = comApprovalListService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 列表查询
	  *
	  * @param comApprovalList
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "审批清单-列表查询")
	 @ApiOperation(value="审批清单-列表查询", notes="审批清单-列表查询")
	 @GetMapping(value = "/listAll")
	 @PermissionData(pageComponent="modules/oa/ComApprovalList/ComApprovalList")
	 public Result<List<ComApprovalList>> queryPageList(ComApprovalList comApprovalList, HttpServletRequest req) {

		 QueryWrapper<ComApprovalList> queryWrapper = QueryGenerator.initQueryWrapper(comApprovalList, req.getParameterMap());

		 List<ComApprovalList> list = comApprovalListService.list(queryWrapper);

		 return Result.OK(list);
	 }

	
	/**
	 *   添加
	 *
	 * @param comApprovalList
	 * @return
	 */
	@AutoLog(value = "审批清单-添加")
	@ApiOperation(value="审批清单-添加", notes="审批清单-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:com_approval_list:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ComApprovalList comApprovalList) {
		comApprovalListService.save(comApprovalList);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param comApprovalList
	 * @return
	 */
	@AutoLog(value = "审批清单-编辑")
	@ApiOperation(value="审批清单-编辑", notes="审批清单-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:com_approval_list:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ComApprovalList comApprovalList) {
		comApprovalListService.updateById(comApprovalList);
		return Result.OK("编辑成功!");
	}


	 /**
	  *  审批
	  *
	  * @param obj
	  * @return
	  */
	 @AutoLog(value = "审批清单-审批")
	 @ApiOperation(value="审批清单-审批", notes="审批清单-审批")
	 @RequestMapping(value = "/shenpi", method = {RequestMethod.PUT,RequestMethod.POST})
	 @Transactional(rollbackFor = Exception.class)
	 public Result<String> shenpi(@RequestBody Map<String,Object> obj) {

		 String ids = obj.getOrDefault("ids", null)+"";
		 Assert.check(! StringUtils.isNullOrEmpty(ids),"参数ids缺失");

		 Object status = obj.getOrDefault("status", "0");
		 Assert.check(status!=null,"参数status缺失");

		 Integer statusInt = Integer.parseInt(status+"") ;

		 Assert.check(statusInt.equals(1)||statusInt.equals(2),"参数status值不合法");

		 String signResult = statusInt.equals(1)?"通过":"驳回";
		 String yiJian = signResult.equals("通过") ? "同意" : "驳回";

		 List<String> idsList = Arrays.asList(ids.split(","));

		 List<ComApprovalList> comApprovalList = comApprovalListService.getByIds(idsList);

		 for(ComApprovalList item : comApprovalList){
			 item.setSignResult(signResult);
			 item.setSignYijian(yiJian);
			 comApprovalListService.updateById(item);

			 comApprovalListService.shenpi(item );
		 }

		 return Result.OK("审批成功!");
	 }


	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "审批清单-通过id删除")
	@ApiOperation(value="审批清单-通过id删除", notes="审批清单-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:com_approval_list:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		comApprovalListService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "审批清单-批量删除")
	@ApiOperation(value="审批清单-批量删除", notes="审批清单-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:com_approval_list:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.comApprovalListService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "审批清单-通过id查询")
	@ApiOperation(value="审批清单-通过id查询", notes="审批清单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ComApprovalList> queryById(@RequestParam(name="id",required=true) String id) {
		ComApprovalList comApprovalList = comApprovalListService.getById(id);
		if(comApprovalList==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(comApprovalList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param comApprovalList
    */
    //@RequiresPermissions("org.jeecg.modules.demo:com_approval_list:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ComApprovalList comApprovalList) {
        return super.exportXls(request, comApprovalList, ComApprovalList.class, "审批清单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("com_approval_list:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ComApprovalList.class);
    }

}
